// Copyright (c) 2009 - Quildreen <quildreen@gmail.com>                     
//                                                                          
//  Permission is hereby granted, free of charge, to any person              
//  obtaining a copy of this software and associated documentation files     
//  (the "Software"), to deal in the Software without restriction,           
//  including without limitation the rights to use, copy, modify, merge,     
//  publish, distribute, sublicense, and/or sell copies of the Software,     
//  and to permit persons to whom the Software is furnished to do so,        
//  subject to the following conditions:                                     
//                                                                           
//  The above copyright notice and this permission notice shall be           
//  included in all copies or substantial portions of the Software.          
//                                                                           
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,          
//  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF       
//  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND                    
//  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE   
//  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION   
//  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION    
//  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

/**
 * lily.graphics.dom.font
 * 
 * Font rendering for DOM surfaces.
 * 
 * Requires: `graphics.font`
 **/
lily.require("graphics.font");
lily.onLoad(function() {
 
    lily.defineModule("graphics.dom.font");


    /**
     * class lily.graphics.dom.Font
     * 
     * DOM system-level font rendering.
     **/
    lily.graphics.dom.Font = Class.create(lily.graphics.Font,
    {
        /**
         * new lily.graphics.dom.Font(name, face)
         **/
        initialize: function($super, name, face)
        {
            $super(name, arguments[3]);
            
            this.font || (this.font = face);
        },


        /**
         * lily.graphics.dom.Font#render(text[, surface][, options]) -> lily.graphics.Surface
         * - text (String): the text to render.
         * - surface ([[lily.graphics.Surface]]): the surface to render the
         *   text to.  
         * - options (Object): additional rendering options.
         * 
         * Renders the given text to the given surface. If no surface is
         * provided, one will be created.
         **/
        render: function(text, surface, options)
        {    
            var font;

            options = Object.extend({
                pos: [0, 0],
                size: [-1, -1],
                bold: this.bold,
                italic: this.italic,
                underline: this.underline,
                overline: this.overline,
                strike: this.strike,
                size: this.size,
                letter_spacing: this.letter_spacing,
                line_height: this.line_height,
                word_wrap: this.word_wrap,
                colour: this.colour,
                text_align: this.text_align
            }, options);

            if (options.bold) {
                if (options.italic) {
                    font = this.bold_italic_font || this.font;
                } else {
                    font = this.bold_font || this.font;
                }
            } else if (options.italic) {
                font = this.italic_font || this.font;
            } else {
                font = this.font;
            }
        
            var decoration = [];
            if (options.underline) {
                decoration.push("underline");
            }
            if (options.overline) {
                decoration.push("overline");
            }
            if (options.strike) {
                decoration.push("line-through");
            }
        
            var elm = new Element("div");
            var style = elm.style;
            style.display = "none";
            style.fontWeight = (options.bold ? "bold":"normal");
            style.fontStyle = (options.italic ? "italic":"normal");
            style.fontFamily = font;
            style.fontSize = options.size + "pt";
            style.color = options.colour || "#fff"; /* fff CSS properties */
            style.letterSpacing = options.letter_spacing;
            style.lineHeight = (options.line_height * 100) + "%";
            style.whiteSpace = (options.word_wrap ? "pre-wrap":"nowrap");
            style.textAlign = options.text_align;
            style.textDecoration = decoration.pop() || "none";
            style.position = "absolute";
            style.left =  options.pos[0] + "px";
            style.top = options.pos[1] + "px";
            elm.update(text.stripTags());
       
            while (decoration.length > 0) {
                elm = new Element("div");
                elm.style.textDecoration = decoration.pop() || "none";
                elm.udpate(elm);
            }

            lily.sm.Application.parent.insert(elm.hide());

            var l = elm.getLayout();
            var w = l.get('width') + options.pos[0];
            var h = l.get('height') + options.pos[1];
            var rx = true;
            var ry = true;
            if (options.size[0] < 0) {
                rx = false;
            } 
            if (options.size[1] < 0) {
                ry = false;
            }

            surface || (surface = new lily.graphics.dom.Surface(null, w, h));
            surface._canvas.insert(elm.remove());
            elm.show().style.display = "block";
            if (rx || ry) {
                surface.setSize((rx ? w : surface.width),
                                (ry ? h : surface.height));
            }

            return surface;
        }
    });

}); // onLoad